package cn.sciento.config.client.api.impl;

import java.util.List;

import cn.sciento.config.client.api.ConfigClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

public class CompositeConfigClient implements ConfigClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(CompositeConfigClient.class);

    private final List<ConfigClient> clients;

    public CompositeConfigClient(List<ConfigClient> clients) {
        if (CollectionUtils.isEmpty(clients))
            LOGGER.warn("no config client, config management does not take effect!");
        this.clients = clients;
    }

    public void publishConfig(String env, String serviceName, String version, String fileType, String content) {
        this.clients.forEach(client -> {
            try {
                client.publishConfig(env, serviceName, version, fileType, content);
            } catch (Throwable e) {
                LOGGER.error("publish config failed", e);
            }
        });
    }
}
